package com.lkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.domain.Area;
import com.lkd.domain.Node;
import com.lkd.domain.User;
import com.lkd.domain.VendingMachine;
import com.lkd.mapper.AreaMapper;
import com.lkd.mapper.NodeMapper;
import com.lkd.mapper.UserMapper;
import com.lkd.mapper.VendingMachineMapper;
import com.lkd.pageParam.RegionDetailsPageParam;
import com.lkd.pageParam.VendingMachinePageParam;
import com.lkd.service.INodeService;
import com.lkd.service.IRegionService;
import com.lkd.service.IVendingMachineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.vo.NodeDetailsVo;
import com.lkd.vo.RegionDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-09
 */
@Service
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineMapper, VendingMachine> implements IVendingMachineService {
    @Autowired
    VendingMachineMapper vendingMachineMapper;
    @Autowired
    NodeMapper nodeMapper;
    @Autowired
    INodeService nodeService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AreaMapper areaMapper;
    @Override
    public Page<NodeDetailsVo> search(VendingMachinePageParam param) {

        //查询设备和对应设备类型的信息,前端传来的信息中innerCode是为了确定设备的,如果innerCode为空，则查询所有设备
        List<NodeDetailsVo> list = nodeMapper.getMvInfoByInnerCode(param.getInnerCode());

        //查询node字段的信息
        list.stream().forEach(nodeDetailsVo -> {
            RegionDetailsPageParam regionDetailsPageParam = new RegionDetailsPageParam();
            regionDetailsPageParam.setRegionId(nodeDetailsVo.getRegionId());
            Page<RegionDetailsVo> page = nodeService.getRegionDetailsVoPage(regionDetailsPageParam);
            //records中有多个节点，但是我们只需要本设备对应的节点
            List<RegionDetailsVo> records = page.getRecords();
            for (RegionDetailsVo regionDetailsVo : records) {
                for (Node node : regionDetailsVo.getRegion().getNodeList()) {
                    Long nodeId = node.getId();
                    if (nodeDetailsVo.getNodeId().equals(nodeId)){
                        nodeDetailsVo.setNode(regionDetailsVo);
                        //找到对应的节点直接结束
                        break;
                    }
                }
            }
        });
        Page<NodeDetailsVo> page = new Page<>(param.getPageIndex(),param.getPageSize());
        page.setTotal(list.size()).setRecords(list);
        return page;
    }

    @Override
    public void insert(VendingMachine vendingMachine) {
        User user = userMapper.selectById(vendingMachine.getCreateUserId());
        vendingMachine.setRegionId(user.getRegionId());
        Node node = nodeMapper.selectById(vendingMachine.getNodeId());
        vendingMachine.setBusinessId(node.getBusinessId());
        vendingMachine.setOwnerId(node.getOwnerId());
        LambdaQueryWrapper<Area> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Area::getAdCode,node.getAreaCode());
        Area area = areaMapper.selectOne(wrapper);
        vendingMachine.setCityCode(area.getCityCode());
        vendingMachine.setAreaId(area.getId());
        vendingMachine.setCreateUserName(user.getUserName());
        save(vendingMachine);
    }
}
